/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.line.pipe.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.line.pipe.dto.CoordinateDTO;
import com.hopes.bizz.line.pipe.dto.LinePipelineDTO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.log.annotation.SysLog;
import com.hopes.bizz.line.pipe.entity.LinePipelineEntity;
import com.hopes.bizz.line.pipe.dto.LinePipelineExcelDTO;
import com.hopes.bizz.line.pipe.vo.LinePipelineExcelVO;
import com.hopes.bizz.line.pipe.service.LinePipelineService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.hopes.boss.common.excel.annotation.RequestExcel;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 管线
 *
 * @author pig
 * @date 2024-02-21 21:45:43
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/pipeline" )
@Tag(description = "pipeline" , name = "管线管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class LinePipelineController {

    private final LinePipelineService linePipelineService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param linePipeline 管线
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('line_pipeline_view')" )
    public R getLinePipelinePage(Page page, LinePipelineDTO linePipeline) {
        final LambdaQueryWrapper<LinePipelineEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StrUtil.isNotBlank(linePipeline.getEmbedType()), LinePipelineEntity::getEmbedType, linePipeline.getEmbedType());
        wrapper.eq(StrUtil.isNotBlank(linePipeline.getLineMaterial()), LinePipelineEntity::getLineMaterial, linePipeline.getLineMaterial());
        wrapper.like(StrUtil.isNotBlank(linePipeline.getKeyword()), LinePipelineEntity::getLineCode, linePipeline.getKeyword())
                .or().like(StrUtil.isNotBlank(linePipeline.getKeyword()), LinePipelineEntity::getStartNum, linePipeline.getKeyword())
                .or().like(StrUtil.isNotBlank(linePipeline.getKeyword()), LinePipelineEntity::getEndNum, linePipeline.getKeyword());
        return R.ok(linePipelineService.pageVo(page, wrapper));
    }


    /**
     * 通过id查询管线
     * @param id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('line_pipeline_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(linePipelineService.getById(id));
    }

    /**
     * 新增管线
     * @param linePipeline 管线
     * @return R
     */
    @Operation(summary = "新增管线" , description = "新增管线" )
    @SysLog("新增管线" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('line_pipeline_add')" )
    public R save(@RequestBody LinePipelineEntity linePipeline) {
        return R.ok(linePipelineService.save(linePipeline));
    }

    /**
     * 修改管线
     * @param linePipeline 管线
     * @return R
     */
    @Operation(summary = "修改管线" , description = "修改管线" )
    @SysLog("修改管线" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('line_pipeline_edit')" )
    public R updateById(@RequestBody LinePipelineEntity linePipeline) {
        return R.ok(linePipelineService.updateById(linePipeline));
    }

    /**
     * 通过id删除管线
     * @param ids id 列表
     * @return R
     */
    @Operation(summary = "通过id删除管线" , description = "通过id删除管线" )
    @SysLog("通过id删除管线" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('line_pipeline_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(linePipelineService.removeBatchByIds(CollUtil.toList(ids)));
    }

    @Operation(summary = "管线统计" , description = "管线统计" )
    @SysLog("管线统计" )
    @PostMapping("statistic")
    @PreAuthorize("@pms.hasPermission('line_pipeline_view')" )
    public R pipeLineStatistic(@RequestBody List<CoordinateDTO> coordinateList) {
        return R.ok(linePipelineService.pipeLineStatistic(coordinateList));
    }


    /**
     * excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/template")
    @PreAuthorize("@pms.hasPermission('line_pipeline_export')")
    public List<LinePipelineExcelVO> template() {
        return linePipelineService.exportExcel(Collections.singletonList(new LinePipelineEntity()));
    }

    /**
     * 导出excel 表格
     * @param linePipeline 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('line_pipeline_export')")
    public List<LinePipelineExcelVO> export(LinePipelineDTO linePipeline) {
        final LambdaQueryWrapper<LinePipelineEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StrUtil.isNotBlank(linePipeline.getEmbedType()), LinePipelineEntity::getEmbedType, linePipeline.getEmbedType());
        wrapper.eq(StrUtil.isNotBlank(linePipeline.getLineMaterial()), LinePipelineEntity::getLineMaterial, linePipeline.getLineMaterial());
        wrapper.like(StrUtil.isNotBlank(linePipeline.getKeyword()), LinePipelineEntity::getLineCode, linePipeline.getKeyword())
                .or().like(StrUtil.isNotBlank(linePipeline.getKeyword()), LinePipelineEntity::getStartNum, linePipeline.getKeyword())
                .or().like(StrUtil.isNotBlank(linePipeline.getKeyword()), LinePipelineEntity::getEndNum, linePipeline.getKeyword());
        return linePipelineService.exportExcel(linePipelineService.list(wrapper));
    }

    /**
     * 导入excel 表格
     * @param linePipelineExcelDtoList 查询条件
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('line_pipeline_export')")
    public R importExcel(@RequestExcel List<LinePipelineExcelDTO> linePipelineExcelDtoList, BindingResult bindingResult) {
        return linePipelineService.importExcel(linePipelineExcelDtoList, bindingResult);
    }
}
